package com.fyp.client.ptma.utils;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.protocol.HTTP;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetworkConnectivity
{
	private static final HttpClient httpclient = new DefaultHttpClient();

	public static boolean isConntectedToInternet(Context context)
	{

		ConnectivityManager connectivity = (ConnectivityManager) context
				.getApplicationContext().getSystemService(
						Context.CONNECTIVITY_SERVICE);
		if (connectivity != null)
		{
			NetworkInfo[] infos = connectivity.getAllNetworkInfo();
			if (infos != null)
				for (NetworkInfo info : infos)
					if (info.getState() == NetworkInfo.State.CONNECTED)
					{
						return true;
					}

		}
		return false;

	}

	public static String getGcmKey()
	{
		String key = null;
		return key;
	}

	public static HttpResponse performPostOperation(String url,
			StringBuilder xml)
	{
		HttpPost httppost = new HttpPost(url);
		BasicHttpResponse httpResponse = null;
		StringEntity se = null;
		try
		{
			se = new StringEntity(xml.toString(), HTTP.UTF_8);
			se.setContentType("text/xml");
			httppost.setHeader("Content-Type", "application/xml;charset=UTF-8");

			httppost.setEntity(se);

			httpResponse = (BasicHttpResponse) httpclient.execute(httppost);
		} catch (UnsupportedEncodingException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return httpResponse;

	}
}
